import 'dart:async';
import 'dart:io';

import 'package:flutter_2b_client/apis/AuthrizationApi.dart';
import 'package:flutter_2b_client/utils/LogOut.dart';
import 'package:mobx/mobx.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../apis/EmployeeApi.dart';

// Include generated file
part 'EmployeeAuthorizationStore.g.dart';

// This is the class used by rest of your codebase
class EmployeeAuthorizationStore = EmployeeAuthorizationStoreBase
    with _$EmployeeAuthorizationStore;

// The store-class
abstract class EmployeeAuthorizationStoreBase with Store {

  @observable
  MeResponse? me;

  @observable
  String? authorization;


  EmployeeAuthorizationStoreBase(){
    (SharedPreferences.getInstance()).then((value) {
      var string = value.getString(HttpHeaders.authorizationHeader);
      if(string!=null){
        setAuthorization(string);
      }
    });
    reaction((p0) => authorization, (p0) {
      (SharedPreferences.getInstance()).then((value) {
        if(p0==null){
          value.remove(HttpHeaders.authorizationHeader);
        }else {
          value.setString(HttpHeaders.authorizationHeader, p0);
        }
      });
    });
  }

  @action
  void setMe(MeResponse me) {
    this.me = me;
  }

  @action
  void setAuthorization(String authorization){
    this.authorization = authorization;
  }

  @action
  logOut() async{
    await authorizationApi.logOut();
    me = null;
    removeAuthorizationHeader();
  }

  @computed
  bool get hasLogin{
    return authorization != null;
  }

}